package club.banyuan;

import java.util.ArrayList;
import java.util.List;

public class Team {
    private List<Player> starting = new ArrayList<>();
    private List<Player> substitute = new ArrayList<>();

    public List<Player> getStarting() {
        return starting;
    }

    public void setStarting(List<Player> starting) {
        this.starting = starting;
    }

    public List<Player> getSubstitute() {
        return substitute;
    }

    public void setSubstitute(List<Player> substitute) {
        this.substitute = substitute;
    }

    // 换人（球员）
    public void Replace(Player player) {
        if (!substitute.contains(player) || player==null){
            System.out.println("查无此人");
            return;
        }
        int indexOfSubstitute = substitute.indexOf(player);
        Player temp = player;
        int indexOfStarting = 0;
        for (Player player1 : starting) {
            if (player.getPos().equals(player1.getPos())) {
                indexOfStarting = starting.indexOf(player1);
                substitute.set(indexOfSubstitute, player1);
            }
        }
        starting.set(indexOfStarting, temp);

    }


    // 添加球员到替补席 添加球员(球员)
    public void addToSubstitute(Player player) {
        substitute.add(player);
    }

    public void adjust(List<Player> list) {
        for (Player player : list) {
            Replace(player);
        }
    }

    public void deletePlayer(Player player) {
        if (starting.contains(player)) {
            starting.remove(player);
        } else
            substitute.remove(player);
    }

    @Override
    public String toString() {
        String startingResult = "首发阵容：", substituteResult = "替补阵容：";
        for (Player player : starting) {
            startingResult += player.getName() + "、";
        }
        for (Player player : substitute) {
            substituteResult += player.getName() + "、";
        }
        return startingResult + "\n" + substituteResult;
    }
}
